package oms.dao;

import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import oms.entities.User;

@Repository
public class UserDAOImpl implements UserDAO {
	
		@Autowired 
    		private SessionFactory sessionFactory;
	
		// OK
		public void createUser(User user) {
		
				sessionFactory.getCurrentSession().save(user);
		}
		
		// OK
		public void updateUser(Long userId) {
			
				User user = (User) sessionFactory.getCurrentSession().load(User.class, userId);
			
				if(null != user) sessionFactory.getCurrentSession().update(user);
			}

		// OK
		public User getUserById(Long userId) {
			
				User user = (User) sessionFactory.getCurrentSession().load(User.class, userId);
		
				return user;
		}

		// OK
		public void deleteUser(Long userId) {
		
				User user = (User) sessionFactory.getCurrentSession().load(User.class, userId);
		
        				if (null != user) sessionFactory.getCurrentSession().delete(user);
		}

		// OK
		@SuppressWarnings("unchecked")
		public List<User> usersList() {
		
				return sessionFactory.getCurrentSession().createQuery("from User").list();
		}

}